//
//  LocalModel.m
//  万物记
//
//  Created by Ajen on 2019/6/16.
//  Copyright © 2019 Ajen. All rights reserved.
//

#import "LocalModel.h"

@implementation LocalModel

+(LocalModel*)shared{
    static LocalModel *sharedAccountManagerInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedAccountManagerInstance = [[self alloc] init];
    });
    return sharedAccountManagerInstance;
}

-(NSArray *)titlesArray{
    NSMutableArray * muArray = [NSMutableArray arrayWithCapacity:0];
    for(int i = 0 ; i < self.model.Anything.count ; i++){
        [muArray addObject:self.model.Anything[i].tabTitle];
    }
    return muArray;
}

-(void)renameTabWithTitle:(NSString *)title andCurrentIndex:(NSInteger)currentindex{
    NSMutableArray<LocalJsonModelAnything *> * anythingMuArr = [NSMutableArray arrayWithCapacity:0];
    [anythingMuArr addObjectsFromArray:self.model.Anything];
    
    anythingMuArr[currentindex].tabTitle = title;
    
    self.model.Anything = anythingMuArr;
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:self.model.mj_keyValues forKey:@"LocalJsonDic"];
    
}


-(void)addTabWithTitle:(NSString *)title andIndex:(NSInteger)index{
    LocalJsonModelAnything * anything = [LocalJsonModelAnything new];
    anything.tabTitle = title;
    anything.contentsList = @[];
    
    NSMutableArray<LocalJsonModelAnything *> * anythingMuArr = [NSMutableArray arrayWithCapacity:0];
    [anythingMuArr addObjectsFromArray:self.model.Anything];
    [anythingMuArr insertObject:anything atIndex:index];
    self.model.Anything = anythingMuArr;
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:self.model.mj_keyValues forKey:@"LocalJsonDic"];
}

-(void)addNewTabInLastWithTitle:(NSString *)title{
    LocalJsonModelAnything * anything = [LocalJsonModelAnything new];
    anything.tabTitle = title;
    anything.contentsList = @[];
    
    NSMutableArray<LocalJsonModelAnything *> * anythingMuArr = [NSMutableArray arrayWithCapacity:0];
    [anythingMuArr addObjectsFromArray:self.model.Anything];
    [anythingMuArr addObject:anything];
    
    self.model.Anything = anythingMuArr;
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:self.model.mj_keyValues forKey:@"LocalJsonDic"];
}

-(void)changeTabFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex{
    NSMutableArray<LocalJsonModelAnything *> * anythingMuArr = [NSMutableArray arrayWithCapacity:0];
    [anythingMuArr addObjectsFromArray:self.model.Anything];
    
    if(toIndex >= anythingMuArr.count){
        toIndex = anythingMuArr.count - 1;
    }
    
    anythingMuArr = [[anythingMuArr AJJumpToIndex:fromIndex with:toIndex] mutableCopy];
    self.model.Anything = anythingMuArr;
    
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:self.model.mj_keyValues forKey:@"LocalJsonDic"];
}

-(void)removeTabWithIndes:(NSInteger)index{
    NSMutableArray<LocalJsonModelAnything *> * anythingMuArr = [NSMutableArray arrayWithCapacity:0];
    [anythingMuArr addObjectsFromArray:self.model.Anything];
    
    if(index >= anythingMuArr.count){
        index = anythingMuArr.count - 1;
    }
    
    [anythingMuArr removeObjectAtIndex:index];
    self.model.Anything = anythingMuArr;
    
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:self.model.mj_keyValues forKey:@"LocalJsonDic"];
}


-(void)addNewContent:(LocalJsonModelContentslist *)contentItem inTabIndex:(NSInteger)tabIndex andInContentListIndex:(NSInteger)contentListIndex{
    NSMutableArray<LocalJsonModelContentslist*> * contentsMuArray = [NSMutableArray arrayWithCapacity:0];
    [contentsMuArray addObjectsFromArray:self.model.Anything[tabIndex].contentsList];
    
    [contentsMuArray insertObject:contentItem atIndex:contentListIndex];
    
    self.model.Anything[tabIndex].contentsList = contentsMuArray;
    
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:self.model.mj_keyValues forKey:@"LocalJsonDic"];
}

-(void)editContent:(LocalJsonModelContentslist *)contentItem belongTabIndex:(NSInteger)tabIndex andEditContentListIndex:(NSInteger)contentListIndex{
    NSMutableArray<LocalJsonModelContentslist*> * contentsMuArray = [NSMutableArray arrayWithCapacity:0];
    
    [contentsMuArray addObjectsFromArray:self.model.Anything[tabIndex].contentsList];
    
    contentsMuArray[contentListIndex] = contentItem;
    
    self.model.Anything[tabIndex].contentsList = contentsMuArray;
    
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:self.model.mj_keyValues forKey:@"LocalJsonDic"];
}

-(void)deleteContentWithTabIndex:(NSInteger)tabIndex andEditContentListIndex:(NSInteger)contentListIndex{
    NSMutableArray<LocalJsonModelContentslist*> * contentsMuArray = [NSMutableArray arrayWithCapacity:0];
    
    [contentsMuArray addObjectsFromArray:self.model.Anything[tabIndex].contentsList];
    
    [contentsMuArray removeObjectAtIndex:contentListIndex];
    
    self.model.Anything[tabIndex].contentsList = contentsMuArray;
    
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:self.model.mj_keyValues forKey:@"LocalJsonDic"];
}

-(void)changeContentWithTabIndex:(NSInteger)tabIndex changeFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex{
    
    NSMutableArray<LocalJsonModelContentslist*> * contentsMuArray = [NSMutableArray arrayWithCapacity:0];
    
    [contentsMuArray addObjectsFromArray:self.model.Anything[tabIndex].contentsList];
    
    if(toIndex >= contentsMuArray.count){
        toIndex = contentsMuArray.count - 1;
    }
    
    contentsMuArray = [contentsMuArray.aj_jumpToIndex(fromIndex,toIndex) mutableCopy];
    
    self.model.Anything[tabIndex].contentsList = contentsMuArray;
    
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:self.model.mj_keyValues forKey:@"LocalJsonDic"];
}
@end
